<?php
// +----------------------------------------------------------------------
// | 网站首页
// +----------------------------------------------------------------------

namespace Home\Controller;
use Common\Controller\Base;
use Common\Model\Model;

class IndexController extends Base {

    protected $indextjwork=array();
    protected $indextjnews=array();


    public function index(){

        //测试用,清空缓存
        cache('',null);

        //幻灯片推荐作品
        $workModel=D('Home/Works');
        $this->assign('indextjwork',$workModel->indextjwork());

        //新闻模块
        $this->assign('indextjnews',D('Home/News')->indextjnews());
        //获取新闻的分类
        $newsClass = M('NewsClass')->field("id,name")->where(array('ifindex'=>1,'style'=>1))->limit(3)->select();
        $this->assign('newsClass',$newsClass);

        //每日新秀
        //$this->assign('indexxinxiu',$workModel->worksxinxiu());

        //中间广告位
        $this->assign('indexmidad',parent::getAdvert(1,1,0,5));

        //左侧广告位
        $this->assign('indexleftad',parent::getAdvert(1,2,0,1));
		
        //类别每日一荐
        $this->assign('indexcatetj',$workModel->cateonework());

        //焙友动态
        //$this->userDynamicData=D('Home/User')->userDynamic(5);

        //热门活动
        //$this->assign('activitytj',D('Home/Activity')->activitytj(2));

        //右边上侧
        $this->assign('rightopad',parent::getAdvert(1,3,0,1));

        //右边广告
        $this->assign('indexrightad',parent::getAdvert(1,3,1,3));

        //分享达人
        $this->assign('sharedaren',D('Home/User')->sharedaren(5,$this->userid));

        //判断是否签到
        $stime=strtotime(date('Y-m-d 00:00:00'));
        $etime=strtotime(date('Y-m-d 23:59:59'));
        $where['userid']= array('EQ',$this->userid);
        $where['create_time']=array(array('GT',$stime),array('LT',$etime),'and');
        $where['action']=array('EQ','sign');

        $map['create_time']=array(array('GT',$stime),array('LT',$etime),'and');
        $map['action']=array('EQ','sign');
        $this->mysign=getCount('UserDynamic',$where);
        $this->signcount=getCount('UserDynamic',$map);

        //随手拍
        $suipai = M('Suipai')->field('title,id,image')->where(array('status'=>true))->order('create_time DESC')->limit(8)->select();
        $this->assign('suipai',$suipai);

        //作品的总数
        $count = M('Works')->count();
        $this->assign('count',$count);

        //获取作品
        $works = M('Works')->field('workid,title,image,copynum')->where(array('type'=>1,'status'=>1))->order('copynum DESC')->limit(3)->select();
       
        foreach ($works as $key => $value) {
           $value['count'] = M('Works')->where(array('copyid'=>$value['workid'],'type'=>2))->count();
           $resources[$value['workid']] = M('Works')->field('workid,image,copyid')->where(array('copyid'=>$value['workid']))->order('clicknum DESC')->limit(3)->select();
           $workes[] = $value;
        }
        
        foreach ($workes as $key => $value) {
            $value['son'] = $resources[$value['workid']];
            $wokRes[] = $value;
        }
        $this->assign('wokRes',$wokRes);
        
        $timeWorks = M('Works')->alias('W')
                    ->field('U.openuid,U.nickname,U.uavatar,W.workid,W.image,W.title')
                    ->join('__USER__ U ON U.userid = W.userid')
                    ->where(array('W.type'=>1,'W.status'=>1))
                    ->order('W.create_time DESC')->limit(3)
                    ->select();


       
        $this->assign('timeWorks',$timeWorks);

        $this->display();
    }


    /**
     * 类别列表
     */
    public function category(){
        $cateid=I('get.cid');
        $catfid=I('get.cfid');
        $order = I('get.order');

        if($cateid){
             $where['cateid']=$cateid;
             $cid = $cateid;
        }
        if($catfid){
            $where['catfid']=$catfid;
            $cid =$catfid;
        }

        if($order == 'liked'){
            $orderData = "create_time DESC,workid DESC";
        }
        else{
            $orderData = "clicknum DESC,workid DESC";
            $order='news';
        }
       
       $this->assign('order',$order);

        $where['type'] = 1;
        $where['status']=true;
        $count = M('Works')->where($where)->count();
        $page = $this->Pages($count, 15);
        $pageurl='?page='.urlencode('[PAGE]');
        $page->url     = __APP__.'/category/'.$cateid.'/'.$pageurl;
        $this->pagecount=$page->totalRows/$page->listRows;
        
        $list=D('Home/Works')->relation(array('User','Category'))->where($where)->order($orderData)->limit($page->firstRow . ',' . $page->listRows)->select();

        //$this->assign('catename',$list[0]['catename']);
        $this->assign('list',$list);
        $this->assign("Page", $page->show());

        //获取分类
        $cat = M('Category')->where(array('cateid'=>$cid))->find();
        $this->assign('cat',$cat);

        //$this->assign('midad',parent::getAdvert(3,1,0,1));
        //右上角广告
        $this->assign('righttopad',parent::getAdvert(3,3,0,1));

        //热门活动
       // $this->assign('activitytj',D('Home/Activity')->activitytj(2));

        //焙友动态
        //$this->userDynamicData=D('Home/User')->userDynamic(5);

        //分享达人
       //$this->assign('sharedaren',D('Home/User')->sharedaren(5,$this->userid));

        $wklist = M('Works')->order('clicknum DESC,workid DESC')->limit(5)->select();
        $this->assign('wklist',$wklist);

        //活动列表
        $activity = M('Activity')->where(array('status'=>1))->limit(5)->order('sort asc')->select();
        $this->assign('activity',$activity);
        
        $this->display();
    }





}